package com.jishuo.nontax.bfc.service;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.config.NonTaxConfig;
import com.jishuo.base.service.SequenceService;
import com.jishuo.base.util.CheckNullStr;
import com.jishuo.nontax.bean.bank.Item;
import com.jishuo.nontax.bean.bank.ItemXml;
import com.jishuo.nontax.bean.vo.ItemVO;
import com.jishuo.nontax.bfc.dao.ItemMapper;
import com.jishuo.nontax.bfc.tlq.MessageFactory;
import com.jishuo.nontax.bfc.tlq.MessageService;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther dinggaofei
 * @Date 2022/12/15 11:04
 */
@Service
public class ItemService {
    private static Logger logger = LoggerFactory.getLogger(ItemService.class);
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private MessageService messageService;
    @Resource
    NonTaxConfig nonTaxConfig;


    public List<ItemVO> findItem(Long cantonid, Long unitid){
        return itemMapper.findItem(cantonid,unitid);
    }
    public List<Item> findUnitVsItemView(Long unitid){
        return itemMapper.findUnitVsItem(unitid);
    }


    /**
     * @param unitid 单位的唯一标识
     * @auther dinggaofei
     * @Date 2022/12/13 15:02
     * @description 查询预算单位可用的收费项目
     */
    public BaseResult findUnitVsItem(Long unitid) {

        try {
            ArrayList<Item> list = (ArrayList<Item>) itemMapper.findUnitVsItem(unitid);
            //查询返回记录
            if (list != null && list.size() > 0) {
                //xml解析器
                Serializer serializer = new Persister();
                //xml转换器
                StringWriter sw = new StringWriter();
                ItemXml xml = new ItemXml();
                list = CheckNullStr.listNullToString(list);
                xml.setItem(list);
                serializer.write(xml, sw);
                return new BaseResult(sw.toString());
            } else {
                return new BaseResult("<bills></bills>");
            }
        } catch (Exception e) {
            logger.info("失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }

    /**
     * @param itemcode 项目编号
     * @auther dinggaofei
     * @Date 2022/12/15 14:03
     * @description 根据项目编号查询预算单位可用的收费项目
     */
    public BaseResult selNewItem(String itemcode) {

        try {
            long messageid = sequenceService.getNextSeq("ELEMESSAGESEQ");

            //xml解析器
            Serializer serializer = new Persister();
            //xml转换器
            StringWriter sw = new StringWriter();
            ItemXml xml = new ItemXml();
            //发送到财政
            boolean suc = messageService.sendAndRecvMessage("GETITEM", messageid, MessageFactory.getNewItemDTO(itemcode), "2.3");
            if (suc) {
                //获取缴款书
                ArrayList<Item> list = (ArrayList<Item>) itemMapper.selectItemByCode(itemcode);
                //查询返回记录
                if (list != null && list.size() > 0) {
                    xml.setItem(list);
                    serializer.write(xml, sw);
                    return new BaseResult(sw.toString());
                }
            }
            return new BaseResult("<bills></bills>");
        } catch (Exception e) {
            logger.info(" 处理代理银行信息失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }


    public BaseResult getNewItem(String itemcode){
        try {
            long messageid = sequenceService.getNextSeq("ELEMESSAGESEQ");

            Object newItemDTO = MessageFactory.getNewItemDTO(itemcode);

            //发送到财政
            messageService.sendMessage("GETITEM", messageid, newItemDTO, "2.3");
            return new BaseResult();

        } catch (Exception e) {
            logger.info(" 处理代理银行信息失败："+e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }

}
